
alter table "MasterBill"
drop constraint "FK_Receipt_ModulesMasterId";

alter table  "MasterBill"
add column if not exists "LocationId" integer null;
alter table  "MasterBill"
add column if not exists "ReceiptAreaTypeId" integer null;

update  "MasterBill"
set "ReceiptAreaTypeId"=1 where "ModulesMasterId"=6;

update  "MasterBill"
set "ReceiptAreaTypeId"=3 where "ModulesMasterId"=17;

update  "MasterBill"
set "ReceiptAreaTypeId"=4 where "ModulesMasterId"=16;

update  "MasterBill"
set "ReceiptAreaTypeId"=10 where "ModulesMasterId"=2;

update  "MasterBill"
set "ReceiptAreaTypeId"=8 where "ModulesMasterId"=1;


-----------------------------------------------------------------------------------------
DROP FUNCTION IF EXISTS public."Masterbills_AppointmentReports"(date, date, integer, integer);

CREATE OR REPLACE FUNCTION public."Masterbills_AppointmentReports"(
	"fromDate" date DEFAULT NULL::date,
	"toDate" date DEFAULT NULL::date,
	locationid integer DEFAULT NULL::integer,
	moduleid integer DEFAULT NULL::integer)
    RETURNS TABLE("BillNumber" character varying, "BillDate" timestamp without time zone, "BillStatusTypeId" integer, "BillStatus" character varying, "Total" numeric, "Discount" numeric, "NetTotal" numeric,"Rounding" numeric, "Receipts" text, "Receipts_Cost" text, "ReceiptSum" numeric, "ReceiptAreaTypeId" integer, "PatientName" text, "Mobile" character varying, "UMRNo" character varying, "ProviderName" character varying, "LocationId" integer, "ModuleName" character varying, "LocationName" character varying, "AppointmentNo" character varying, "AppointmentDate" timestamp without time zone,"OPConsultation" numeric,"ScanConsultation" numeric,"Lab" numeric,"Pharmacy" numeric,"RegistrationCharges" numeric) 
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE PARALLEL UNSAFE
    ROWS 1000

AS $BODY$
begin
return query

with getreceipts as (
	select  MB."MasterBillId",MB."BillNumber",string_agg(R."ReceiptId"::text,',')   AS "Receipts",
string_agg(R."Cost"::text,',')  AS "Receipts_Cost",MB."ReceiptAreaTypeId",--MB."ModulesMasterId",
	sum(R."Cost") as "ReceiptSum"
	from "MasterBill" MB 
	 join  "Receipt" R on R."MasterBillId"=MB."MasterBillId"	
	group by (MB."MasterBillId")
	--order by MB."BillNumber" asc
)
,appointment as(
select 
	distinct on (MB."MasterBillId")
 MB."BillNumber",MB."MasterBillId",MB."BillDate",MB."BillStatusTypeId",BST."Name" as "BillStatus",
MB."Total",MB."Discount",MB."NetTotal",MB."Rounding",--GR."Receipts",GR."Receipts_Cost",GR."ReceiptSum",
P."FullName" as "PatientName",P."Mobile",P."UMRNo",pr."FullName" as "ProviderName",
AT."LocationId",MM."Name" as "ModuleName",L."Name" as "LocationName",
AP."AppointmentNo",AP."AppointmentDate"
FROM "MasterBill" MB
 Join "Appointment" AP on AP."AppointmentId"=MB."ModuleId"
	join "Provider" pr on pr."ProviderId"= AP."ProviderId"
 join "BillStatusType" BST on BST."BillStatusTypeId"=MB."BillStatusTypeId"
 join "Patient" P on P."PatientId"=MB."PatientId"
 --join "ModulesMaster" MM on MM."ModulesMasterId"=MB."ModulesMasterId"	
join "ReceiptAreaType" MM on MM."ReceiptAreaTypeId"=MB."ReceiptAreaTypeId"	
 join "AppointmentTransaction" AT on AT."AppointmentId"=AP."AppointmentId"-- and MB."ReceiptAreaTypeId"=4
 join "Location" L on L."LocationId"=AT."LocationId"	
where  
case when "fromDate" is null then 1=1 else "fromDate"<= AP."AppointmentDate"::date end 
 		and case when "toDate" is null then 1=1 else AP."AppointmentDate"::date<=  "toDate" end 
		 and case when "locationid" is null then 1=1 else AT."LocationId" = "locationid" end
)
,lab as (
SELECT distinct on (MB."MasterBillId")
MB."BillNumber",MB."MasterBillId",MB."BillDate",MB."BillStatusTypeId",BST."Name" as "BillStatus",
MB."Total",MB."Discount",MB."NetTotal",MB."Rounding",--GR."Receipts",GR."Receipts_Cost",GR."ReceiptSum",
P."FullName" as "PatientName",P."Mobile",P."UMRNo",prv."FullName" as "ProviderName",
	L."Name" as "LocationName",nlbh."LocationId",MM."Name" as "ModuleName",
 nlbh."RequisitionNumber" as "AppointmentNo",nlbh."CreatedDate" as "AppointmentDate"
FROM "MasterBill" MB
			join "NewLabBookingHeader" nlbh on nlbh."NewLabBookingHeaderId"=MB."ModuleId"
			join "Provider" prv on prv."ProviderId" = nlbh."DoctorId"    
			join "BillStatusType" BST on BST."BillStatusTypeId"=MB."BillStatusTypeId"                                                                   
			join "Patient" P on P."PatientId"=MB."PatientId"
			--join "ModulesMaster" MM on MM."ModulesMasterId"=MB."ModulesMasterId"   
	join "ReceiptAreaType" MM on MM."ReceiptAreaTypeId"=MB."ReceiptAreaTypeId"	
			-- join "getreceipts" GR on GR."MasterBillId"=MB."MasterBillId"
 			join "AppointmentTransaction" AT on AT."AppointmentId"=nlbh."NewLabBookingHeaderId"
		    join "Location" L on L."LocationId"=AT."LocationId"
		where 	
	case when "fromDate" is null then 1=1 else "fromDate"<= nlbh."CreatedDate"::date end 
 		and case when "toDate" is null then 1=1 else nlbh."CreatedDate"::date<=  "toDate" end 
		 and case when "locationid" is null then 1=1 else AT."LocationId" = "locationid" end																		  								   
)
,pharma as (
SELECT distinct on (MB."MasterBillId")
MB."BillNumber",MB."MasterBillId",MB."BillDate",MB."BillStatusTypeId",BST."Name" as "BillStatus",
MB."Total",MB."Discount",MB."NetTotal",MB."Rounding",--GR."Receipts",GR."Receipts_Cost",GR."ReceiptSum",
P."FullName" as "PatientName",P."Mobile",P."UMRNo",nlbh."ProviderName",
	L."Name" as "LocationName",nlbh."LocationId",MM."Name" as "ModuleName",
 nlbh."BillNumber" as "AppointmentNo",nlbh."CreatedDate" as "AppointmentDate"		
	FROM "MasterBill" MB
			join "PharmacySaleHeader" nlbh on nlbh."PharmacySaleHeaderId"=MB."ModuleId"                               
			join "BillStatusType" BST on BST."BillStatusTypeId"=MB."BillStatusTypeId"                                                                   
			join "Patient" P on P."PatientId"=MB."PatientId"
			--join "ModulesMaster" MM on MM."ModulesMasterId"=MB."ModulesMasterId" 
	join "ReceiptAreaType" MM on MM."ReceiptAreaTypeId"=MB."ReceiptAreaTypeId"	
			-- join "getreceipts" GR on GR."MasterBillId"=MB."MasterBillId"
 			join "AppointmentTransaction" AT on AT."AppointmentId"=nlbh."PharmacySaleHeaderId"
			join "Location" L on L."LocationId"=AT."LocationId"
	where 
		case when "fromDate" is null then 1=1 else "fromDate"<= nlbh."CreatedDate"::date end 
 		and case when "toDate" is null then 1=1 else nlbh."CreatedDate"::date<=  "toDate" end 
		 and case when "locationid" is null then 1=1 else AT."LocationId" = "locationid" end																		  								   
)
,scan as(
SELECT distinct on (MB."MasterBillId")
MB."BillNumber",MB."MasterBillId",MB."BillDate",MB."BillStatusTypeId",BST."Name" as "BillStatus",
MB."Total",MB."Discount",MB."NetTotal",MB."Rounding",--GR."Receipts",GR."Receipts_Cost",GR."ReceiptSum",
P."FullName" as "PatientName",P."Mobile",P."UMRNo",SM."MachineName" as "ProviderName",
	L."Name" as "LocationName",nlbh."LocationId",MM."Name" as "ModuleName",
 nlbh."RequisitionNumber" as "AppointmentNo",nlbh."AppointmentDate"			
	FROM "MasterBill" MB
			join "BookScanAppointment" nlbh on nlbh."BookScanAppointmentId"=MB."ModuleId" 
			join "ScanMachineMaster" SM on SM."ScanMachineMasterId"=nlbh."ScanMachineMasterId"
			join "BillStatusType" BST on BST."BillStatusTypeId"=MB."BillStatusTypeId"                                                                   
			join "Patient" P on P."PatientId"=MB."PatientId"
			--join "ModulesMaster" MM on MM."ModulesMasterId"=MB."ModulesMasterId"  
	join "ReceiptAreaType" MM on MM."ReceiptAreaTypeId"=MB."ReceiptAreaTypeId"	
			-- join "getreceipts" GR on GR."MasterBillId"=MB."MasterBillId"
 			join "AppointmentTransaction" AT on AT."AppointmentId"=nlbh."BookScanAppointmentId"
		    join "Location" L on L."LocationId"=AT."LocationId"
	where
		case when "fromDate" is null then 1=1 else "fromDate"<= nlbh."AppointmentDate"::date end 
 		and case when "toDate" is null then 1=1 else nlbh."AppointmentDate"::date<=  "toDate" end 
		 and case when "locationid" is null then 1=1 else AT."LocationId" = "locationid" end	
)

SELECT O."BillNumber",O."BillDate",O."BillStatusTypeId",O."BillStatus",
O."Total",O."Discount",O."NetTotal",O."Rounding",O."Receipts",O."Receipts_Cost",O."ReceiptSum",O."ReceiptAreaTypeId",
O."PatientName",O."Mobile",O."UMRNo",O."ProviderName",
O."LocationId",O."ModuleName",O."LocationName",
O."AppointmentNo",O."AppointmentDate",
coalesce(case when O."ReceiptAreaTypeId"=4 then O."ReceiptSum" end,0) as "OPConsultation" ,
coalesce(case when O."ReceiptAreaTypeId"=10 then O."ReceiptSum" end,0) as "ScanConsultation",
coalesce(case when O."ReceiptAreaTypeId"=8 then O."ReceiptSum" end,0) as "Lab" ,
coalesce(case when O."ReceiptAreaTypeId"=1 then O."ReceiptSum" end,0) as "Pharmacy",
coalesce(case when O."ReceiptAreaTypeId"=3 then O."ReceiptSum" end,0) as "RegistrationCharges"
from 
(
	Select  AP."BillNumber",AP."BillDate",AP."BillStatusTypeId",AP."BillStatus",
AP."Total",AP."Discount",AP."NetTotal",AP."Rounding",GR."Receipts",GR."Receipts_Cost",GR."ReceiptSum",GR."ReceiptAreaTypeId",
AP."PatientName",AP."Mobile",AP."UMRNo",AP."ProviderName",
AP."LocationId",AP."ModuleName",AP."LocationName",
AP."AppointmentNo",AP."AppointmentDate"
from getreceipts GR
join "appointment" AP on GR."MasterBillId"=AP."MasterBillId"	
UNION
	Select  AP."BillNumber",AP."BillDate",AP."BillStatusTypeId",AP."BillStatus",
AP."Total",AP."Discount",AP."NetTotal",AP."Rounding",GR."Receipts",GR."Receipts_Cost",GR."ReceiptSum",GR."ReceiptAreaTypeId",
AP."PatientName",AP."Mobile",AP."UMRNo",AP."ProviderName",
AP."LocationId",AP."ModuleName",AP."LocationName",
AP."AppointmentNo",AP."AppointmentDate"
from getreceipts GR
join "lab" AP on GR."MasterBillId"=AP."MasterBillId"
	
	UNION
	Select  AP."BillNumber",AP."BillDate",AP."BillStatusTypeId",AP."BillStatus",
AP."Total",AP."Discount",AP."NetTotal",AP."Rounding",GR."Receipts",GR."Receipts_Cost",GR."ReceiptSum",GR."ReceiptAreaTypeId",
AP."PatientName",AP."Mobile",AP."UMRNo",AP."ProviderName",
AP."LocationId",AP."ModuleName",AP."LocationName",
AP."AppointmentNo",AP."AppointmentDate"
from getreceipts GR
join "pharma" AP on GR."MasterBillId"=AP."MasterBillId"
	UNION
	Select  AP."BillNumber",AP."BillDate",AP."BillStatusTypeId",AP."BillStatus",
AP."Total",AP."Discount",AP."NetTotal",AP."Rounding",GR."Receipts",GR."Receipts_Cost",GR."ReceiptSum",GR."ReceiptAreaTypeId",
AP."PatientName",AP."Mobile",AP."UMRNo",AP."ProviderName",
AP."LocationId",AP."ModuleName",AP."LocationName",
AP."AppointmentNo",AP."AppointmentDate"
from getreceipts GR
join "scan" AP on GR."MasterBillId"=AP."MasterBillId"
		
)O
where 	
	 case when "moduleid" is null then 1=1 else O."ReceiptAreaTypeId" = "moduleid" end	
	 
GROUP BY GROUPING SETS((O."BillNumber",O."BillDate",O."BillStatusTypeId",O."BillStatus",
O."Total",O."Discount",O."NetTotal",O."Rounding",O."Receipts",O."Receipts_Cost",O."ReceiptSum",O."ReceiptAreaTypeId",
O."PatientName",O."Mobile",O."UMRNo",O."ProviderName",
O."LocationId",O."ModuleName",O."LocationName",
O."AppointmentNo",O."AppointmentDate")) 

order by O."ModuleName" asc;

end
$BODY$;

ALTER FUNCTION public."Masterbills_AppointmentReports"(date, date, integer, integer)
    OWNER TO postgres;

---------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------